package com.zuipin.controller;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.DrawLevelGroup;
import com.zuipin.entity.Prize;
import com.zuipin.entity.shoppingCart.RedPacketType;
import com.zuipin.entity.shoppingCart.RuleSet;
import com.zuipin.entity.shoppingCart.RuleType;
import com.zuipin.entity.shoppingCart.ShowType;
import com.zuipin.entity.shoppingCart.Status;
import com.zuipin.enums.PrizeType;
import com.zuipin.enums.TimeDotType;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IRuleSetService;
import com.zuipin.util.ArithUtil;
import com.zuipin.util.DateUtils;
import com.zuipin.util.RegexUtils;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.AddCartRuleVo;
import com.zuipin.vo.AddDrawRuleVo;
import com.zuipin.vo.AddRedPacketRuleVo;
import com.zuipin.vo.AddSpecialRuleVo;
import com.zuipin.vo.AddUserBehaviorRuleVo;
import com.zuipin.vo.DrawLevelGroupVo;
import com.zuipin.vo.EnumVo;
import com.zuipin.vo.RuleListResponseVo;
import com.zuipin.vo.RuleResponseVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @ClassName: RuleSetController
 * @author wuyicheng
 * @time 2017年6月13日上午10:21:58
 */
@Api(value = "规则设置-xujinding", description = "规则设置-xujinding")
@RequestMapping(value = "/ruleSet")
@Controller
public class RuleSetController {
	protected final Logger	log	= Logger.getLogger(getClass());
	@Resource
	private IRuleSetService	ruleSetService;
	
	@RequestMapping(value = "/findRuleList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询规则列表(送抽奖，送红包)-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询规则列表(送抽奖，送红包)-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "ruleType", required = true, regex = "RED_PACKET|DRAW"),
			@Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleListResponseVo>> findRuleList(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "ruleType", value = "规则类型  RED_PACKET|DRAW ", required = true) @RequestParam(required = true) String ruleType,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = new RuleSet();
		rs.setStatus(Status.ALR_ENABLE.toString());
		rs.setRuleType(ruleType);
		rs.setSysId(sysId);
		PageResult<List<RuleListResponseVo>> pov = ruleSetService.findList(rs, pageNumber, pageSize, "", "");
		pov.setErrorCode(ErrorCode.SUCCESS);
		return pov;
	}
	
	@RequestMapping(value = "/findRuleListByIds", method = RequestMethod.POST)
	@ApiOperation(notes = "查询规则列表详情使用-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询规则列表详情使用-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "ids", required = false),
			@Valid(name = "ruleType", required = true, regex = "RED_PACKET|DRAW"), @Valid(name = "status", required = false, regex = "ALR_ENABLE"),
			@Valid(name = "sysId", required = true), @Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleResponseVo>> findRuleListByIds(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "ids", value = "编码", required = false) @RequestParam(required = false) String ids,
			@ApiParam(name = "status", value = "状态", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "ruleType", value = "类型  红包，抽奖 RED_PACKET|DRAW", required = true) @RequestParam(required = true) String ruleType,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		
		PageResult<List<RuleResponseVo>> pov = ruleSetService.findRuleListByIds(ids, ruleType, pageNumber, pageSize, sysId, status);
		return pov;
		
	}
	
	@RequestMapping(value = "/findRuleStatus", method = RequestMethod.POST)
	@ApiOperation(notes = "查询规则状态-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询规则状态-xujinding")
	@ResponseBody
	public Result<List<EnumVo>> findRuleStatus() {
		Result<List<EnumVo>> res = new Result();
		List<EnumVo> list = Status.findRuleStatus();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/findPromotionType", method = RequestMethod.POST)
	@ApiOperation(notes = "查询促销类型-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询促销类型-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "type", required = true, regex = "CART|USER_BEHAVIOR") })
	public Result<List<EnumVo>> findPromotionType(
			@ApiParam(name = "type", value = "类型 购物车  用户行为  CART|USER_BEHAVIOR", required = true) @RequestParam(required = true) String type) {
		Result<List<EnumVo>> res = new Result<>();
		List<EnumVo> list = ShowType.findPromotionType(type);
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/modityStatusRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "修改规则状态-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4020-规则为空"
			+ "</br>7002-只有在未激活或禁用才能激活，只有在激活才能禁用", value = "修改规则状态-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "status", required = true, regex = "ALR_ENABLE|ALR_DISABLE") })
	public Result modityStatusRuleById(@ApiParam(name = "id", value = "id", required = true) @RequestParam(required = true) Long id,
			@ApiParam(name = "updateId", value = "操作者", required = true) @RequestParam(required = true) Long updateId,
			@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "status", value = "状态   ALR_ENABLE:已启用, ALR_DISABLE:已禁用", required = true) @RequestParam(required = true) String status) throws CustomException {
		Result res = new Result();
		ruleSetService.modityStatusRuleById(id, updateId, status, sysId);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@RequestMapping(value = "/findRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询规则详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询规则详情-xujinding")
	@ResponseBody
	public Result<RuleSet> findRuleById(@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id) {
		RuleSet rs = ruleSetService.findRuleById(id);
		Result<RuleSet> res = new Result<RuleSet>();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(rs);
		return res;
	}
	
	/////////////////////// 红包红包红包红包红包////////////////开始//////////////////////
	/////////////////////// 红包红包红包红包红包////////////////开始//////////////////////
	@RequestMapping(value = "/redPacket/addOrModityRule", method = RequestMethod.POST)
	@ApiOperation(notes = "添加红包规则-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>7001-只有未激活才能修改规则"
			+ "</br>4020-规则为空", value = "添加红包规则-xujinding")
	@ResponseBody
	@Valids({
			//
			@Valid(name = "addRedPacketRuleVo.id", required = false, maxLength = 20), @Valid(name = "addRedPacketRuleVo.showName", required = true, maxLength = 25),
			@Valid(name = "addRedPacketRuleVo.showType", required = true, regex = "ORDER_DISCOUNT|CASH|SINGLE_PRO_DISCOUNT"),
			@Valid(name = "addRedPacketRuleVo.device", required = true, regex = "PC|WAP|PC&WAP"),
			@Valid(name = "addRedPacketRuleVo.validPeriodType", required = true, maxValue = 2, minValue = 1),
			@Valid(name = "addRedPacketRuleVo.startTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addRedPacketRuleVo.endTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addRedPacketRuleVo.validDays", required = false, minValue = 0, maxValue = 999999),
			@Valid(name = "addRedPacketRuleVo.remark", required = false, maxLength = 100), @Valid(name = "addRedPacketRuleVo.createId", required = false),
			@Valid(name = "addRedPacketRuleVo.updateId", required = false), @Valid(name = "addRedPacketRuleVo.sysId", required = true),
			@Valid(name = "addRedPacketRuleVo.amount", required = false, minValue = 0), @Valid(name = "addRedPacketRuleVo.discount", required = false, maxValue = 10, minValue = 0),
			@Valid(name = "addRedPacketRuleVo.createNum", required = true, minValue = 0, maxValue = 999999), @Valid(name = "addRedPacketRuleVo.fullAmountUse", required = true),
			@Valid(name = "addRedPacketRuleVo.proTypeLimit", required = true, regex = "NO_LIMIT|LIMIT_PRO"),
			@Valid(name = "addRedPacketRuleVo.isLimitGetNum", required = true, maxValue = 1, minValue = 0), @Valid(name = "addRedPacketRuleVo.limitGetNum", required = false),
			@Valid(name = "addRedPacketRuleVo.limitMemberLevel", required = true),
			@Valid(name = "addRedPacketRuleVo.startDrawTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addRedPacketRuleVo.endDrawTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addRedPacketRuleVo.brandType", required = false), @Valid(name = "addRedPacketRuleVo.catType", required = false),
			@Valid(name = "addRedPacketRuleVo.sku", required = false), @Valid(name = "addRedPacketRuleVo.isBrandJoin", required = false, maxValue = 1, minValue = 0),
			@Valid(name = "addRedPacketRuleVo.isCatJoin", required = false, maxValue = 1, minValue = 0),
			@Valid(name = "addRedPacketRuleVo.isSkuJoin", required = false, maxValue = 2, minValue = 0),
			@Valid(name = "addRedPacketRuleVo.limitExplain", required = false, maxLength = 100),
			//
	})
	public Result addOrModityRedPacketRule(@RequestBody @ApiParam(value = "添加红包规则") AddRedPacketRuleVo addRedPacketRuleVo)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, CustomException {
		Result res = new Result<>();
		// 判断现金券的参数
		if (RedPacketType.CASH.toString().equals(addRedPacketRuleVo.getShowType())) {
			if (!(addRedPacketRuleVo.getAmount() != null && addRedPacketRuleVo.getAmount().doubleValue() > 0)) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("现金券，金额要大于0");
				res.setData(null);
				return res;
			}
		} else if (RedPacketType.ORDER_DISCOUNT.toString().equals(addRedPacketRuleVo.getShowType())
				|| RedPacketType.SINGLE_PRO_DISCOUNT.toString().equals(addRedPacketRuleVo.getShowType())) {
			// 打折券的参数
			if (!(addRedPacketRuleVo.getDiscount() != null && addRedPacketRuleVo.getDiscount().doubleValue() > 0 && addRedPacketRuleVo.getDiscount().doubleValue() < 10)) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("打折券，折数大于0小于10");
				res.setData(null);
				return res;
			}
		}
		// 判断有效期限制
		if (RuleSet.VALID_TIME_RANGE.equals(addRedPacketRuleVo.getValidPeriodType())) {
			if (StringUtil.isBlank(addRedPacketRuleVo.getStartTime()) || StringUtil.isBlank(addRedPacketRuleVo.getEndTime())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("有效期限制参数错误");
				res.setData(null);
				return res;
			}
		} else {
			if (!(addRedPacketRuleVo.getValidDays() != null && addRedPacketRuleVo.getValidDays() > 0)) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("有效期天数要不能小于0");
				res.setData(null);
				return res;
			}
		}
		
		// 判断是不是修改 是的话 updateId要有
		if (addRedPacketRuleVo.getId() != null && addRedPacketRuleVo.getUpdateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("修改时，必须有更新者");
			res.setData(null);
			return res;
		}
		
		// 判断是不是新增 是的话 createId要有
		if (addRedPacketRuleVo.getId() == null && addRedPacketRuleVo.getCreateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("新增时，必须有创建者");
			res.setData(null);
			return res;
		}
		// sku 校验
		if ((RuleSet.SKU_INCLUDE_NO.equals(addRedPacketRuleVo.getIsSkuJoin()) || RuleSet.SKU_INCLUDE_YES.equals(addRedPacketRuleVo.getIsSkuJoin()))
				&& StringUtil.isBlank(addRedPacketRuleVo.getSku())) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("sku没填");
			res.setData(null);
			return res;
		}
		
		res = ruleSetService.addOrModityRedPacketRule(addRedPacketRuleVo);
		return res;
	}
	
	@RequestMapping(value = "/redPacket/findType", method = RequestMethod.POST)
	@ApiOperation(notes = "查询红包类型-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询红包类型-xujinding")
	@ResponseBody
	public Result<List<EnumVo>> findRedPacketType() {
		Result<List<EnumVo>> res = new Result<>();
		List<EnumVo> list = ShowType.findRedPacketType();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/redPacket/findRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询红包规则详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询红包规则详情-xujinding")
	@ResponseBody
	public Result<AddRedPacketRuleVo> findRedPacketRuleById(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = ruleSetService.findRuleById(id);
		Result<AddRedPacketRuleVo> res = new Result<AddRedPacketRuleVo>();
		if (rs != null && RuleType.RED_PACKET.toString().equals(rs.getRuleType()) && sysId.equals(rs.getSysId())) {
			AddRedPacketRuleVo vo = new AddRedPacketRuleVo();
			PropertyUtils.copyProperties(vo, rs);
			res.setData(vo);
		} else {
			res.setData(null);
		}
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@RequestMapping(value = "/redPacket/findRuleList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询红包规则列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询红包规则列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "status", required = false, regex = "NOT_ACTIVATE|ALR_ENABLE|ALR_DISABLE"),
			@Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleListResponseVo>> findRedRuleList(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "status", value = "状态   NOT_ACTIVATE:未激活, ALR_ENABLE:已启用, ALR_DISABLE:已禁用", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "showName", value = "名称", required = false) @RequestParam(required = false) String showName,
			// @ApiParam(name = "showCode", value = "编码", required = false) @RequestParam(required = false) String showCode,
			@ApiParam(name = "id", value = "编码", required = false) @RequestParam(required = false) String id,
			@ApiParam(name = "remark", value = "备注", required = false) @RequestParam(required = false) String remark,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = new RuleSet();
		if (id != null) {
			try {
				Long longId = Long.valueOf(id);
				rs.setId(longId);
			} catch (Exception e) {
				PageResult<List<RuleListResponseVo>> pov = new PageResult<List<RuleListResponseVo>>();
				pov.setData(new ArrayList<RuleListResponseVo>());
				pov.setTotal_record(0);
				pov.setErrorCode(ErrorCode.SUCCESS);
				return pov;
			}
		}
		// rs.setId(id);
		rs.setShowName(showName);
		rs.setStatus(status);
		rs.setRemark(remark);
		rs.setSysId(sysId);
		PageResult<List<RuleListResponseVo>> pov = ruleSetService.findRedRuleList(rs, pageNumber, pageSize, "", "");
		pov.setErrorCode(ErrorCode.SUCCESS);
		return pov;
	}
	
	/////////////////////// 特价特价特价特价////////////////开始//////////////////////
	/////////////////////// 特价特价特价特价////////////////开始//////////////////////
	@RequestMapping(value = "/special/addOrModityRule", method = RequestMethod.POST)
	@ApiOperation(notes = "添加特价规则-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>7001-只有未激活才能修改规则"
			+ "</br>4020-规则为空", value = "添加特价规则-xujinding")
	@ResponseBody
	@Valids({
			//
			@Valid(name = "addSpecialRuleVo.id", required = false, maxLength = 20), @Valid(name = "addSpecialRuleVo.showName", required = true, maxLength = 25),
			@Valid(name = "addSpecialRuleVo.device", required = true, regex = "PC|WAP|PC&WAP"),
			@Valid(name = "addSpecialRuleVo.startTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addSpecialRuleVo.endTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addSpecialRuleVo.tagline", required = false, maxLength = 25), @Valid(name = "addSpecialRuleVo.createId", required = false),
			@Valid(name = "addSpecialRuleVo.updateId", required = false), @Valid(name = "addSpecialRuleVo.sysId", required = true),
			@Valid(name = "addSpecialRuleVo.isStopPrivilege", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addSpecialRuleVo.isLimitBuyBill", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addSpecialRuleVo.limitBuyBill", required = false, maxValue = 999999999, minValue = 0),
			@Valid(name = "addSpecialRuleVo.isLimitBuyNum", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addSpecialRuleVo.numTop", required = false, maxValue = 999999999, minValue = 0),
			@Valid(name = "addSpecialRuleVo.numDown", required = false, maxValue = 999999999, minValue = 0), @Valid(name = "addSpecialRuleVo.limitMemberLevel", required = true),
			@Valid(name = "addSpecialRuleVo.proRuleList.proSku", required = true), @Valid(name = "addSpecialRuleVo.proRuleList.specialPrice", required = true),
			@Valid(name = "addSpecialRuleVo.proRuleList.discount", required = true), @Valid(name = "addSpecialRuleVo.proRuleList.subAmount", required = true) })
	public Result addOrModitySpecialRule(@RequestBody @ApiParam(value = "添加特价规则") AddSpecialRuleVo addSpecialRuleVo)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, CustomException {
		Result res = new Result();
		try {
			if (DateUtils.parseFullTime(addSpecialRuleVo.getStartTime()).getTime() > DateUtils.parseFullTime(addSpecialRuleVo.getEndTime()).getTime()) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("有效期结束时间要大于开始时间");
				res.setData(null);
				return res;
			}
		} catch (ParseException e) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("时间格式不正确");
			res.setData(null);
			return res;
		}
		
		// 判断是不是修改 是的话 updateId要有
		if (addSpecialRuleVo.getId() != null && addSpecialRuleVo.getUpdateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("修改时，必须有更新者");
			res.setData(null);
			return res;
		}
		
		// 判断是不是新增 是的话 createId要有
		if (addSpecialRuleVo.getId() == null && addSpecialRuleVo.getCreateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("修改时，必须有创建者");
			res.setData(null);
			return res;
		}
		
		// 限购单数
		if (RuleSet.YES.equals(addSpecialRuleVo.getIsLimitBuyBill())) {
			if (addSpecialRuleVo.getLimitBuyBill() == null) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("限购单数，数量不能为空");
				res.setData(null);
				return res;
			}
		}
		/*		// 限制购买数量时 校验参数
				if (RuleSet.YES.equals(addSpecialRuleVo.getIsLimitBuyNum())) {
					if (!(addSpecialRuleVo.getNumTop() != null && addSpecialRuleVo.getNumDown() != null
							&& addSpecialRuleVo.getNumDown().doubleValue() <= addSpecialRuleVo.getNumTop().doubleValue())) {
						res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
						res.setMessage("限购数量，数量不能为空，且最多要大于等于最少");
						res.setData(null);
						return res;
					}
				}*/
		
		// 校验购买条件 ---购买数量
		if (addSpecialRuleVo.getNumDown() != null && addSpecialRuleVo.getNumTop() != null
				&& addSpecialRuleVo.getNumDown().doubleValue() > addSpecialRuleVo.getNumTop().doubleValue()) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("最小数量大于最大数量");
			res.setData(null);
			return res;
		}
		if (RuleSet.NO.equals(addSpecialRuleVo.getIsLimitBuyNum()) && (addSpecialRuleVo.getNumDown() != null || addSpecialRuleVo.getNumTop() != null)) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("不限制不必要填数量");
			res.setData(null);
			return res;
		}
		
		res = ruleSetService.addOrModitySpecialRule(addSpecialRuleVo);
		return res;
	}
	
	@RequestMapping(value = "/special/findRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询特价规则详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询特价规则详情-xujinding")
	@ResponseBody
	public Result<AddSpecialRuleVo> findSpecialRuleDetailById(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		AddSpecialRuleVo addSpecialRuleVo = ruleSetService.findSpecialRuleDetailById(id, sysId);
		Result<AddSpecialRuleVo> res = new Result<AddSpecialRuleVo>();
		res.setData(addSpecialRuleVo);
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@RequestMapping(value = "/special/findRuleList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询特价规则列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询特价规则列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "showName", required = false),
			@Valid(name = "status", required = false, regex = "NOT_ACTIVATE|ALR_ENABLE|ALR_DISABLE"), @Valid(name = "sku", required = false),
			@Valid(name = "sysId", required = true), @Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleListResponseVo>> findSpecialRuleList(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "status", value = "状态   NOT_ACTIVATE:未激活, ALR_ENABLE:已启用, ALR_DISABLE:已禁用", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "showName", value = "名称", required = false) @RequestParam(required = false) String showName,
			// @ApiParam(name = "showCode", value = "编码", required = false) @RequestParam(required = false) String showCode,
			@ApiParam(name = "sku", value = "商品sku", required = false) @RequestParam(required = false) String sku,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = new RuleSet();
		// rs.setShowCode(showCode);
		rs.setShowName(showName);
		rs.setStatus(status);
		rs.setSysId(sysId);
		rs.setSku(sku);
		rs.setRuleType(RuleType.SPECIAL_PRICE.toString());
		PageResult<List<RuleListResponseVo>> pov = ruleSetService.findRuleList(rs, pageNumber, pageSize, "", "");
		return pov;
		
	}
	
	/////////////////////// 购物车规则////////////////开始//////////////////////
	/////////////////////// 购物车规则////////////////开始//////////////////////
	
	@RequestMapping(value = "/cart/findTimeDotType", method = RequestMethod.POST)
	@ApiOperation(notes = "查询时间点-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询时间点-xujinding")
	@ResponseBody
	public Result<List<EnumVo>> findTimeDotType() {
		Result<List<EnumVo>> res = new Result();
		List<EnumVo> list = TimeDotType.findTimeDotType();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/cart/addOrModityRule", method = RequestMethod.POST)
	@ApiOperation(notes = "添加购物车规则-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>7001-只有未激活才能修改规则"
			+ "</br>4020-规则为空", value = "添加购物车规则-xujinding")
	@ResponseBody
	@Valids({
			//
			@Valid(name = "addCartRule.id", required = false, maxLength = 20), @Valid(name = "addCartRule.showName", required = true, maxLength = 25),
			@Valid(name = "addCartRule.device", required = true, regex = "PC|WAP|PC&WAP"),
			@Valid(name = "addCartRule.startTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addCartRule.endTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addCartRule.sort", required = true, minValue = 1, maxValue = 999999), @Valid(name = "addCartRule.createId", required = false),
			@Valid(name = "addCartRule.updateId", required = false), @Valid(name = "addCartRule.sysId", required = true),
			@Valid(name = "addCartRule.isStopPrivilege", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.isStopOtherRule", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.joinNum", required = false, maxValue = 999999999, minValue = 0),
			@Valid(name = "addCartRule.isLimitBuyNum", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.isLimitBuyMoney", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.numTop", required = false, maxValue = 999999999, minValue = 0),
			@Valid(name = "addCartRule.numDown", required = false, maxValue = 999999999, minValue = 0),
			@Valid(name = "addCartRule.amountDown", required = false, maxValue = 999999999, minValue = 0),
			@Valid(name = "addCartRule.amountTop", required = false, maxValue = 999999999, minValue = 0), @Valid(name = "addCartRule.limitMemberLevel", required = true),
			@Valid(name = "addCartRule.proTypeLimit", required = true, regex = "NO_LIMIT|LIMIT_PRO"), @Valid(name = "addCartRule.brandType", required = false),
			@Valid(name = "addCartRule.catType", required = false), @Valid(name = "addCartRule.sku", required = false),
			@Valid(name = "addCartRule.isBrandJoin", required = false, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.isCatJoin", required = false, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.isSkuJoin", required = false, maxValue = 2, minValue = 0), @Valid(name = "addCartRule.giveNum", required = false, minValue = 0),
			@Valid(name = "addCartRule.giveCode", required = false),
			@Valid(name = "addCartRule.showType", required = true, regex = "SUB_AMOUNT|GIVE_INTEGRAL|GIVE_TOPUP_BALANCE|GIVE_RED_PACKET|GIVE_PRO|FREE_POSTAGE|GIVE_DRAW_NUM"),
			@Valid(name = "addCartRule.isOverlay", required = false, maxValue = 1, minValue = 0),
			@Valid(name = "addCartRule.freeShippingType", required = false, regex = "FREE_SHIPPING_ORDER|FREE_SHIPPING_PRODUCT"),
			@Valid(name = "addCartRule.freeShippingPayType", required = false), @Valid(name = "addCartRule.freeShippingExpressCode", required = false),
			@Valid(name = "addCartRule.giveTimeDot", required = false, regex = "PAY_SUCCESS|PAY_SUCCESS_NH|ORDER_COMPLETE"),
			@Valid(name = "addCartRule.singleMostNum", required = false) })
	public Result addOrModityCartRule(@RequestBody @ApiParam(value = "添加购物车规则") AddCartRuleVo addCartRule)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, CustomException, ParseException {
		Result<Map<String, Object>> res = new Result<>();
		if (DateUtils.parseFullTime(addCartRule.getStartTime()).getTime() > DateUtils.parseFullTime(addCartRule.getEndTime()).getTime()) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("有效期结束时间要大于开始时间");
			res.setData(null);
			return res;
		}
		
		// 判断是不是修改 是的话 updateId要有
		if (addCartRule.getId() != null && addCartRule.getUpdateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("修改时，必须有更新者");
			res.setData(null);
			return res;
		}
		
		// 判断是不是新增 是的话 createId要有
		if (addCartRule.getId() == null && addCartRule.getCreateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("新增时，必须有创建者");
			res.setData(null);
			return res;
		}
		
		// 校验减金额参数
		if (ShowType.SUB_AMOUNT.toString().equals(addCartRule.getShowType())) {
			if (addCartRule.getAmount() == null) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("促销类型--减金额不能为空");
				res.setData(null);
				return res;
			}
		}
		
		// 校验免邮参数
		if (ShowType.FREE_POSTAGE.toString().equals(addCartRule.getShowType())) {
			if (!(ShowType.FREE_SHIPPING_ORDER.toString().equals(addCartRule.getFreeShippingType())
					|| ShowType.FREE_SHIPPING_PRODUCT.toString().equals(addCartRule.getFreeShippingType()))) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("免邮券类型出错");
				res.setData(null);
				return res;
			}
			
			if (StringUtil.isEmpty(addCartRule.getFreeShippingPayType()) || StringUtil.isEmpty(addCartRule.getFreeShippingExpressCode())
					|| StringUtil.isEmpty(addCartRule.getFreeShippingType())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("免邮参数出错，免邮的类型,免邮的免邮支付类型那,免邮快递方式");
				res.setData(null);
				return res;
			}
		}
		
		// 校验送积分参数//校验送余额参数
		if (ShowType.GIVE_INTEGRAL.toString().equals(addCartRule.getShowType()) || ShowType.GIVE_TOPUP_BALANCE.toString().equals(addCartRule.getShowType())) {
			if (addCartRule.getGiveNum() == null || StringUtil.isEmpty(addCartRule.getGiveTimeDot())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("促销类型--赠送数量或赠送时间点为空");
				res.setData(null);
				return res;
			}
		}
		
		// 校验送红包参数
		if (ShowType.GIVE_RED_PACKET.toString().equals(addCartRule.getShowType())) {
			if (addCartRule.getIsOverlay() == null || addCartRule.getGiveNum() == null || StringUtil.isEmpty(addCartRule.getGiveTimeDot())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("促销类型--是否叠加或赠送数量或赠送时间点为空");
				res.setData(null);
				return res;
			}
		}
		
		if (RuleSet.NO.equals(addCartRule.getIsLimitBuyMoney()) && (addCartRule.getAmountDown() != null || addCartRule.getAmountTop() != null)) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("不限制购买金额就没必要传金额");
			res.setData(null);
			return res;
		}
		
		if (RuleSet.NO.equals(addCartRule.getIsLimitBuyNum()) && (addCartRule.getNumDown() != null || addCartRule.getNumTop() != null)) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("不限制购买数量就没必要传数量");
			res.setData(null);
			return res;
		}
		
		// 校验购买条件 ---购买数量
		if (addCartRule.getNumDown() != null && addCartRule.getNumTop() != null && addCartRule.getNumDown().doubleValue() >= addCartRule.getNumTop().doubleValue()) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("购买条件--最小数量大于或等于最大数量");
			res.setData(null);
			return res;
		}
		
		/*		// 校验购买条件 ---购买金额
				if (RuleSet.YES.equals(addCartRule.getIsLimitBuyMoney()) && addCartRule.getAmountDown() == null) {
					res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					res.setMessage("购买条件--最小金额不能为空");
					res.setData(null);
					return res;
				}*/
		
		if (addCartRule.getAmountDown() != null && addCartRule.getAmountTop() != null && addCartRule.getAmountDown().doubleValue() >= addCartRule.getAmountTop().doubleValue()) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("购买条件--最大金额要大于或等于最小金额");
			res.setData(null);
			return res;
		}
		
		// sku 校验
		if ((RuleSet.SKU_INCLUDE_NO.equals(addCartRule.getIsSkuJoin()) || RuleSet.SKU_INCLUDE_YES.equals(addCartRule.getIsSkuJoin())) && StringUtil.isBlank(addCartRule.getSku())) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("sku没填");
			res.setData(null);
			return res;
		}
		
		// 只有只填最小值 才能叠加
		if (!((addCartRule.getAmountDown() != null && addCartRule.getAmountDown().doubleValue() > 0 && addCartRule.getAmountTop() == null)
				|| (addCartRule.getNumDown() != null && addCartRule.getNumDown().doubleValue() > 0 && addCartRule.getNumTop() == null))) {
			if (RuleSet.YES.equals(addCartRule.getIsOverlay())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("只有只填最小值并且要大于0,才能叠加");
				res.setData(null);
				return res;
			}
		}
		
		if (!(RuleSet.YES.equals(addCartRule.getIsOverlay())) && addCartRule.getSingleMostNum() != null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("不叠加，就没必要传单次最大");
			res.setData(null);
			return res;
		}
		
		// 除了 积分 充值 要有赠送编码
		if (ShowType.GIVE_RED_PACKET.toString().equals(addCartRule.getShowType()) || ShowType.GIVE_DRAW_NUM.toString().equals(addCartRule.getShowType())
				|| ShowType.GIVE_PRO.toString().equals(addCartRule.getShowType())) {
			if (StringUtil.isBlank(addCartRule.getGiveCode())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("赠送编码不能为空");
				res.setData(null);
				return res;
			}
		}
		
		if (ShowType.GIVE_INTEGRAL.toString().equals(addCartRule.getShowType()) || ShowType.GIVE_RED_PACKET.toString().equals(addCartRule.getShowType())
				|| ShowType.GIVE_DRAW_NUM.toString().equals(addCartRule.getShowType()) || ShowType.GIVE_PRO.toString().equals(addCartRule.getShowType())) {
			int temp = addCartRule.getGiveNum().intValue();
			if (temp != addCartRule.getGiveNum().doubleValue()) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("赠送数量不能是小数");
				res.setData(null);
				return res;
			}
		}
		
		res = ruleSetService.addOrModityCartRule(addCartRule);
		
		return res;
	}
	
	@RequestMapping(value = "/cart/findRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询购物车规则详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询购物车规则详情-xujinding")
	@ResponseBody
	public Result<AddCartRuleVo> findCartRuleById(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = ruleSetService.findRuleById(id);
		Result<AddCartRuleVo> res = new Result<AddCartRuleVo>();
		if (rs != null && RuleType.CART.toString().equals(rs.getRuleType()) && sysId.equals(rs.getSysId())) {
			AddCartRuleVo vo = new AddCartRuleVo();
			PropertyUtils.copyProperties(vo, rs);
			res.setData(vo);
		}
		res.setErrorCode(ErrorCode.SUCCESS);
		return res;
	}
	
	@RequestMapping(value = "/cart/findRuleList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询购物车规则列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询购物车规则列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "showName", required = false, maxLength = 25),
			@Valid(name = "status", required = false, regex = "NOT_ACTIVATE|ALR_ENABLE|ALR_DISABLE"), @Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleListResponseVo>> findCartRuleList(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "status", value = "状态   NOT_ACTIVATE:未激活, ALR_ENABLE:已启用, ALR_DISABLE:已禁用", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "showName", value = "名称", required = false) @RequestParam(required = false) String showName,
			// @ApiParam(name = "showCode", value = "编码", required = false) @RequestParam(required = false) String showCode,
			@ApiParam(name = "id", value = "编码", required = false) @RequestParam(required = false) String id,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = new RuleSet();
		
		if (id != null) {
			try {
				Long longId = Long.valueOf(id);
				rs.setId(longId);
			} catch (Exception e) {
				PageResult<List<RuleListResponseVo>> pov = new PageResult<List<RuleListResponseVo>>();
				pov.setData(new ArrayList<RuleListResponseVo>());
				pov.setTotal_record(0);
				pov.setErrorCode(ErrorCode.SUCCESS);
				return pov;
			}
		}
		
		// rs.setShowCode(showCode);
		// rs.setId(id);
		rs.setShowName(showName);
		rs.setStatus(status);
		rs.setSysId(sysId);
		rs.setRuleType(RuleType.CART.toString());
		PageResult<List<RuleListResponseVo>> pov = ruleSetService.findRuleList(rs, pageNumber, pageSize, "", "");
		return pov;
		
	}
	
	//////////////////////// 抽奖规则///////////////////////////////////////////
	//////////////////////// 抽奖规则///////////////////////////////////////////
	
	@RequestMapping(value = "/draw/findPrizeType", method = RequestMethod.POST)
	@ApiOperation(notes = "查询奖品类型-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询奖品类型-xujinding")
	@ResponseBody
	public Result<List<EnumVo>> findPrizeType() {
		Result<List<EnumVo>> res = new Result<>();
		List<EnumVo> list = PrizeType.findPrizeType();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(list);
		return res;
	}
	
	@RequestMapping(value = "/draw/addOrModityRule", method = RequestMethod.POST)
	@ApiOperation(notes = "添加抽奖规则-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>7001-只有未激活才能修改规则" + "</br>7005-概率之和不能超过100%"
			+ "</br>4020-规则为空", value = "抽奖规则-xujinding")
	@ResponseBody
	@Valids({
			//
			@Valid(name = "addDrawRuleVo.id", required = false), @Valid(name = "addDrawRuleVo.showName", required = true, maxLength = 25),
			@Valid(name = "addDrawRuleVo.device", required = true, regex = "PC|WAP|PC&WAP"),
			@Valid(name = "addDrawRuleVo.startTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addDrawRuleVo.endTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2), @Valid(name = "addDrawRuleVo.createId", required = false),
			@Valid(name = "addDrawRuleVo.updateId", required = false), @Valid(name = "addDrawRuleVo.sysId", required = true),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.id", required = false, maxLength = 20), @Valid(name = "addDrawRuleVo.drawLevelGroups.level", required = true),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.levelTitle", required = true),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.winningType", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.id", required = false),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.isDefault", required = true, maxValue = 1, minValue = 0),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.title", required = true),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.oneNum", required = true, minValue = 0),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.prompt", required = false), @Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.prizeNo", required = false), //
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.prizeType", required = true, regex = "GIVE_INTEGRAL|GIVE_TOPUP_BALANCE|GIVE_RED_PACKET|GIVE_PRO|GIVE_OTHER|GIVE_DRAW_NUM"),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.unit", required = false),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.prizeQuantity", required = false, minValue = 0),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.promptUrl", required = false),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.probability", required = true, maxValue = 100, minValue = 0),
			@Valid(name = "addDrawRuleVo.drawLevelGroups.prizes.abbreviation", required = true),
	
	})
	public Result addOrModityDrawRule(@RequestBody @ApiParam(value = "添加抽奖或修改规则") AddDrawRuleVo addDrawRuleVo)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, CustomException {
		Result<Map<String, Object>> res = new Result<>();
		try {
			if (DateUtils.parseFullTime(addDrawRuleVo.getStartTime()).getTime() > DateUtils.parseFullTime(addDrawRuleVo.getEndTime()).getTime()) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("有效期结束时间要大于开始时间");
				res.setData(null);
				return res;
			}
		} catch (ParseException e) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("时间格式不正确");
			res.setData(null);
			return res;
		}
		// 判断是不是修改 是的话 updateId要有
		if (addDrawRuleVo.getId() != null && addDrawRuleVo.getUpdateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("修改时，必须有更新者");
			res.setData(null);
			return res;
		}
		// 新增 是的话 createId要有
		if (addDrawRuleVo.getId() == null && addDrawRuleVo.getCreateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("新增时，必须有创建者");
			res.setData(null);
			return res;
			
		}
		
		// 根据等级的中奖类型来 判断 默认奖品 和概率
		if (addDrawRuleVo.getDrawLevelGroups() == null || addDrawRuleVo.getDrawLevelGroups().size() < 1) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("必须有等级");
			res.setData(null);
			return res;
		}
		
		for (DrawLevelGroupVo dlgv : addDrawRuleVo.getDrawLevelGroups()) {
			BigDecimal probabilitySum = new BigDecimal(0);//
			Short isDefault = 0;// 判断默认
			if (dlgv.getPrizes() == null || dlgv.getPrizes().size() < 1) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage(dlgv.getLevelTitle() + "---奖品为空");
				res.setData(null);
				return res;
			}
			
			for (Prize p : dlgv.getPrizes()) {
				
				if (PrizeType.GIVE_DRAW_NUM.toString().equals(p.getPrizeType()) || PrizeType.GIVE_RED_PACKET.toString().equals(p.getPrizeType())
						|| PrizeType.GIVE_INTEGRAL.toString().equals(p.getPrizeType()) || PrizeType.GIVE_PRO.toString().equals(p.getPrizeType())) {
					int temp = p.getOneNum().intValue();
					if (temp != p.getOneNum().doubleValue()) {
						res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
						res.setMessage(dlgv.getLevelTitle() + "---赠送数量不能是小数");
						res.setData(null);
						return res;
					}
				}
				
				probabilitySum = ArithUtil.add(probabilitySum, p.getProbability(), 2);
				if (PrizeType.GIVE_RED_PACKET.toString().equals(p.getPrizeType())) {
					if (!StringUtil.isBlank(p.getPrizeNo())) {
						String tempStr = p.getPrizeNo().replaceAll(",", "");
						if (!RegexUtils.isINTEGER_NEGATIVE(tempStr)) {
							res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
							res.setMessage(dlgv.getLevelTitle() + "---红包规则编号有错");
							res.setData(null);
							return res;
						}
					} else {
						res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
						res.setMessage(dlgv.getLevelTitle() + "---红包规则编号有错");
						res.setData(null);
						return res;
					}
					
				}
				
				if (PrizeType.GIVE_PRO.toString().equals(p.getPrizeType()) && StringUtil.isBlank(p.getPrizeNo())) {
					res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					res.setMessage(dlgv.getLevelTitle() + "---送商品，sku有错");
					res.setData(null);
					return res;
				}
				
				if (Prize.DEFAULT_YES.equals(p.getIsDefault())) {
					isDefault++;
				} /*
					 * else { if (!(p.getPrizeQuantity() != null && p.getPrizeQuantity().doubleValue() > 0)) { res.setErrorCode(ErrorCode.ILLEGAL_PARAM); res.setMessage("非默认的奖品必须有总数量必填且不能小于0");
					 * res.setData(null); return res; } }
					 */
			}
			
			Short type = dlgv.getWinningType();// 中奖类型
			if (DrawLevelGroup.WINNING_TYPE_0.equals(type)) {
				if ((probabilitySum.doubleValue() < 0 || probabilitySum.doubleValue() > 100)) {
					res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					res.setMessage(dlgv.getLevelTitle() + "---非100%中奖的概率要大于0小于1");
					res.setData(null);
					return res;
				}
				if (isDefault != 0) {
					res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					res.setMessage(dlgv.getLevelTitle() + "---非100%中奖没必要有默认奖品");
					res.setData(null);
					return res;
				}
			} else {
				if (!(probabilitySum.doubleValue() == 100)) {
					res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					res.setMessage(dlgv.getLevelTitle() + "---100%中奖的概率要等于1");
					res.setData(null);
					return res;
				}
				if (isDefault != 1) {
					res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
					res.setMessage(dlgv.getLevelTitle() + "---100%中奖必须有默认奖品，并且只能有一个");
					res.setData(null);
					return res;
				}
			}
		}
		res = ruleSetService.addOrModityDrawRule(addDrawRuleVo);
		return res;
	}
	
	@RequestMapping(value = "/draw/findRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询抽奖规则详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询抽奖规则详情-xujinding")
	@ResponseBody
	public Result<AddDrawRuleVo> findDrawRuleById(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		AddDrawRuleVo rs = ruleSetService.findDrawRuleById(id, sysId);
		Result<AddDrawRuleVo> res = new Result<AddDrawRuleVo>();
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(rs);
		return res;
	}
	
	@RequestMapping(value = "/draw/findRuleList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询抽奖规则列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询抽奖规则列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "showName", required = false, maxLength = 25),
			@Valid(name = "status", required = false, regex = "NOT_ACTIVATE|ALR_ENABLE|ALR_DISABLE"),
			// @Valid(name = "showCode", required = false, maxLength = 200),
			@Valid(name = "serialNumber", required = false, maxLength = 200), @Valid(name = "sysId", required = true), @Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleListResponseVo>> findDrawRuleList(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "status", value = "状态   NOT_ACTIVATE:未激活, ALR_ENABLE:已启用, ALR_DISABLE:已禁用", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "showName", value = "名称", required = false) @RequestParam(required = false) String showName,
			// @ApiParam(name = "showCode", value = "编码", required = false) @RequestParam(required = false) String showCode,
			@ApiParam(name = "id", value = "编码", required = false) @RequestParam(required = false) String id,
			@ApiParam(name = "serialNumber", value = "序列号", required = false) @RequestParam(required = false) String serialNumber,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = new RuleSet();
		// rs.setShowCode(showCode);
		// rs.setId(id);
		if (id != null) {
			try {
				Long longId = Long.valueOf(id);
				rs.setId(longId);
			} catch (Exception e) {
				PageResult<List<RuleListResponseVo>> pov = new PageResult<List<RuleListResponseVo>>();
				pov.setData(new ArrayList<RuleListResponseVo>());
				pov.setTotal_record(0);
				pov.setErrorCode(ErrorCode.SUCCESS);
				return pov;
			}
		}
		rs.setShowName(showName);
		rs.setStatus(status);
		rs.setSysId(sysId);
		rs.setSerialNumber(serialNumber);
		rs.setRuleType(RuleType.DRAW.toString());
		PageResult<List<RuleListResponseVo>> pov = ruleSetService.findRuleList(rs, pageNumber, pageSize, "", "");
		return pov;
	}
	
	//////////////////////// 用户行为管理////////////////////////////////////
	//////////////////////// 用户行为管理////////////////////////////////////
	
	@RequestMapping(value = "/userBehavior/addOrModityRule", method = RequestMethod.POST)
	@ApiOperation(notes = "添加或修改用户行为规则-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>7001-只有未激活才能修改规则"
			+ "</br>4020-规则为空", value = "添加或修改用户行为规则-xujinding")
	@ResponseBody
	@Valids({
			//
			@Valid(name = "addUserBehaviorRuleVo.id", required = false, maxLength = 20), @Valid(name = "addUserBehaviorRuleVo.showName", required = true, maxLength = 25),
			@Valid(name = "addUserBehaviorRuleVo.device", required = true, regex = "PC|WAP|PC&WAP"),
			@Valid(name = "addUserBehaviorRuleVo.startTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addUserBehaviorRuleVo.endTime", required = true, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "addUserBehaviorRuleVo.createId", required = false), @Valid(name = "addUserBehaviorRuleVo.updateId", required = false),
			@Valid(name = "addUserBehaviorRuleVo.sysId", required = true),
			@Valid(name = "addUserBehaviorRuleVo.showType", required = true, regex = "GIVE_INTEGRAL|GIVE_PRO|GIVE_TOPUP_BALANCE|GIVE_RED_PACKET|GIVE_DRAW_NUM"),
			@Valid(name = "addUserBehaviorRuleVo.limitMemberLevel", required = false), @Valid(name = "addUserBehaviorRuleVo.activityType", required = true, regex = "REG|LOGIN"),
			@Valid(name = "addUserBehaviorRuleVo.giveNum", required = true, minValue = 0, maxValue = 999999), @Valid(name = "addUserBehaviorRuleVo.giveCode", required = false)
	
	})
	public Result addOrModityUserBehaviorRule(@RequestBody @ApiParam(value = "添加或修改用户行为规则") AddUserBehaviorRuleVo addUserBehaviorRuleVo)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, CustomException {
		Result<Map<String, Object>> res = new Result<>();
		
		try {
			if (DateUtils.parseFullTime(addUserBehaviorRuleVo.getStartTime()).getTime() > DateUtils.parseFullTime(addUserBehaviorRuleVo.getEndTime()).getTime()) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("有效期结束时间要大于开始时间");
				res.setData(null);
				return res;
			}
		} catch (ParseException e) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("时间格式不正确");
			res.setData(null);
			return res;
		}
		// 判断是不是修改 是的话 updateId要有
		if (addUserBehaviorRuleVo.getId() != null && addUserBehaviorRuleVo.getUpdateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("修改时，必须有更新者");
			res.setData(null);
			return res;
		}
		// 新增 是的话 createId要有
		if (addUserBehaviorRuleVo.getId() == null && addUserBehaviorRuleVo.getCreateId() == null) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("新增时，必须有创建者");
			res.setData(null);
			return res;
			
		}
		// 除了 积分 充值 要有赠送编码
		if (!(ShowType.GIVE_INTEGRAL.toString().equals(addUserBehaviorRuleVo.getShowType())
				|| ShowType.GIVE_TOPUP_BALANCE.toString().equals(addUserBehaviorRuleVo.getShowType()))) {
			if (StringUtil.isBlank(addUserBehaviorRuleVo.getGiveCode())) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("赠送编码不能为空");
				res.setData(null);
				return res;
			}
		}
		
		if (!ShowType.GIVE_TOPUP_BALANCE.toString().equals(addUserBehaviorRuleVo.getShowType())) {
			int temp = addUserBehaviorRuleVo.getGiveNum().intValue();
			if (temp != addUserBehaviorRuleVo.getGiveNum().doubleValue()) {
				res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				res.setMessage("赠送数量不能是小数");
				res.setData(null);
				return res;
			}
		}
		
		// 登录必传
		if (ShowType.LOGIN.toString().equals(addUserBehaviorRuleVo.getActivityType()) && StringUtil.isBlank(addUserBehaviorRuleVo.getLimitMemberLevel())) {
			res.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			res.setMessage("类型是登录，必须传类型");
			res.setData(null);
			return res;
		}
		
		res = ruleSetService.addOrModityUserBehaviorRule(addUserBehaviorRuleVo);
		return res;
	}
	
	@RequestMapping(value = "/userBehavior/findRuleById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询用户行为规则详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询用户行为规则详情-xujinding")
	@ResponseBody
	public Result<AddUserBehaviorRuleVo> findUserBehaviorRuleById(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		// RuleSet rs = ruleSetService.findUserBehaviorRuleById(id);
		Result<AddUserBehaviorRuleVo> res = new Result<AddUserBehaviorRuleVo>();
		RuleSet rs = ruleSetService.findRuleById(id);
		if (rs != null && RuleType.USER_BEHAVIOR.toString().equals(rs.getRuleType()) && sysId.equals(rs.getSysId())) {
			AddUserBehaviorRuleVo vo = new AddUserBehaviorRuleVo();
			PropertyUtils.copyProperties(vo, rs);
			res.setData(vo);
		}
		return res;
	}
	
	@RequestMapping(value = "/userBehavior/findRuleList", method = RequestMethod.POST)
	@ApiOperation(notes = "查询用户行为规则列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询用户行为规则列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "showName", required = false, maxLength = 25),
			@Valid(name = "status", required = false, regex = "NOT_ACTIVATE|ALR_ENABLE|ALR_DISABLE"),
			@Valid(name = "activityType", required = false, maxLength = 200, regex = "LOGIN|REG"), @Valid(name = "sysId", required = true),
			@Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RuleListResponseVo>> findUserBehaviorRuleList(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "status", value = "状态   NOT_ACTIVATE:未激活, ALR_ENABLE:已启用, ALR_DISABLE:已禁用", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "showName", value = "名称", required = false) @RequestParam(required = false) String showName,
			// @ApiParam(name = "showCode", value = "编码", required = false) @RequestParam(required = false) String showCode,
			@ApiParam(name = "id", value = "编码", required = false) @RequestParam(required = false) String id,
			@ApiParam(name = "activityType", value = "活动类型 LOGIN 登录|REG 注册", required = false) @RequestParam(required = false) String activityType,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RuleSet rs = new RuleSet();
		// rs.setShowCode(showCode);
		// rs.setId(id);
		if (id != null) {
			try {
				Long longId = Long.valueOf(id);
				rs.setId(longId);
			} catch (Exception e) {
				PageResult<List<RuleListResponseVo>> pov = new PageResult<List<RuleListResponseVo>>();
				pov.setData(new ArrayList<RuleListResponseVo>());
				pov.setTotal_record(0);
				pov.setErrorCode(ErrorCode.SUCCESS);
				return pov;
			}
		}
		rs.setShowName(showName);
		rs.setShowType(activityType);
		rs.setStatus(status);
		rs.setSysId(sysId);
		rs.setActivityType(activityType);
		rs.setRuleType(RuleType.USER_BEHAVIOR.toString());
		PageResult<List<RuleListResponseVo>> pov = ruleSetService.findRuleList(rs, pageNumber, pageSize, "", "");
		return pov;
	}
	
}
